package com.ailiwu.www.ailiwu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.activity.PostRecylerActivity;
import com.ailiwu.www.ailiwu.bean.ClassifySubjectSc;
import com.ailiwu.www.ailiwu.url.Urls;
import com.ailiwu.www.ailiwu.utils.Logger;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zzw on 2015/12/4.
 */
public class StrategyRecyclerViewAdapter extends RecyclerView.Adapter<StrategyRecyclerViewAdapter.ViewHolder> {

    private LayoutInflater mInflater;
    private ClassifySubjectSc cfss;
    private ImageLoader imageLoader;
    private List<ClassifySubjectSc.DataEntity.CollectionsEntity> collections;
    private DisplayImageOptions options;
    private static Context mContext;


    public StrategyRecyclerViewAdapter(Context context, Object datas) {
        mInflater = LayoutInflater.from(context);

        mContext = context;

        collections = new ArrayList<>();
        if (datas != null) {
            cfss = (ClassifySubjectSc) datas;
            collections = cfss.getData().getCollections();
            Logger.i("-=-=-=-="+collections.toString());
            imageLoader = ImageLoader.getInstance();
            imageLoader.init(ImageLoaderConfiguration.createDefault(context));
            options = new DisplayImageOptions.Builder()
                    .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
//                .displayer(new FadeInBitmapDisplayer(100))// 渐入的方式进入
                    .displayer(new RoundedBitmapDisplayer(10))
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .build();
        }
    }


    @Override
    public int getItemCount() {
        return collections == null ? 0 : collections.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.item_strategy,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);


        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
        imageLoader.displayImage(collections.get(i).getBanner_image_url(), viewHolder.mImg, options);
        //每项设置监听
        viewHolder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PostRecylerActivity.class);
                intent.putExtra("title",collections.get(i).getTitle());
                intent.putExtra("urlID", Urls.strategySubUrl(collections.get(i).getId()));
                mContext.startActivity(intent);
            }
        });
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView mImg;
        View view;

        public ViewHolder(View view) {
            super(view);
            this.view = view;
            mImg = (ImageView) view
                    .findViewById(R.id.iv_item_strategy);


        }

    }
}